﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Elentok.Common.Wpf.Dialogs
{
  /// <summary>
  /// Interaction logic for PasswordDialog.xaml
  /// </summary>
  public partial class PasswordDialog : Window
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="PasswordDialog"/> class.
    /// </summary>
    public PasswordDialog()
    {
      InitializeComponent();

      this.Loaded += delegate { this.passwordTextBox.Focus(); };
    }

    /// <summary>
    /// Gets or sets the error message.
    /// </summary>
    /// <value>The error message.</value>
    public string ErrorMessage
    {
      get { return this.errorMessageLabel.Text; }
      set
      {
        this.errorMessageLabel.Text = value;
        this.errorMessageLabel.Visibility = string.IsNullOrEmpty(value) ? Visibility.Hidden : Visibility.Visible;
      }
    }

    /// <summary>
    /// Gets the password.
    /// </summary>
    /// <value>The password.</value>
    public string Password
    {
      get
      {
        if (!Validate())
          return null;

        return this.passwordTextBox.Password;
      }
    }

    private bool Validate()
    {
      this.okButton.IsEnabled = !string.IsNullOrEmpty(this.passwordTextBox.Password);

      return this.okButton.IsEnabled;
    }

    private void passwordTextBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
      Validate();
    }

    private void okButton_Click(object sender, RoutedEventArgs e)
    {
      DialogResult = true;
      Close();
    }
 }
}
